package com.health.mapp.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse.PlayInfo;
import com.health.commons.PropertiesService;
import com.health.db.model.GoodsCourseItem;
import com.health.db.service.sm.IGoodsCourseItemService;
import com.health.db.service.sm.impl.GoodsCourseItemServiceImpl;
import com.health.mapp.bean.GetVideoPlayUrlReq;
import com.health.mapp.bean.GetVideoPlayUrlResp;
import com.health.mapp.bean.Header;
import com.health.mapp.utils.Response;
import com.health.mapp.utils.ResultCode;

/**
 * Created by YYY-Work on 2017/6/2.
 */
public class GetVideoPlayUrlImpl implements ActionInterface {

	@Autowired
	IGoodsCourseItemService goodsCourseItemService = new GoodsCourseItemServiceImpl();
	
    @Override
    public Response execute(Header header, Object reqBean) throws Exception {

        GetVideoPlayUrlReq req = (GetVideoPlayUrlReq) reqBean;
        GetVideoPlayUrlResp _resp=new GetVideoPlayUrlResp();

        DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", PropertiesService.getString("video.accesskey"), PropertiesService.getString("video.secret"));
        DefaultAcsClient client = new DefaultAcsClient(profile);
        
        GoodsCourseItem goodsCourseItem = goodsCourseItemService.selectByPrimaryKey(Long.valueOf(req.getRecId()));
        
        if (null!=goodsCourseItem&&null!=goodsCourseItem.getVideoId()) {
        	GetPlayInfoResponse response=getPlayInfo(client,goodsCourseItem.getVideoId());
            if (null!=response)
            {
            	//头图
            	goodsCourseItem.setMainImageUrl(response.getVideoBase().getCoverURL());
            	List<PlayInfo> infos=response.getPlayInfoList();
            	
            	for (int i = 0; i < infos.size(); i++) {
            		PlayInfo temp = infos.get(i);
            		
            		if (temp.getFormat().equals("mp4")) {
            			//播放资源
            			goodsCourseItem.setSouceUrl(temp.getPlayURL());
            			break;
					}
				}
                _resp.setErrorCode(ResultCode.OPERATION_SUCCESS);
                _resp.setErrorMsg(ResultCode.OPERATION_SUCCESS_NOTE);
                _resp.setCourseItem(goodsCourseItem);
            }else
            {
                _resp.setErrorCode(-1);
                _resp.setErrorMsg("获取视频信息失败");
            }
		}
   
        return _resp;
    }

    GetPlayInfoResponse getPlayInfo(DefaultAcsClient client,String vid){
    	GetPlayInfoRequest request= new GetPlayInfoRequest();
    	 request.setVideoId(vid);
    	GetPlayInfoResponse response = null;
    	 try {
    	 response = client.getAcsResponse(request);
    	 
    } catch (ServerException e) {
    	e.printStackTrace();
        throw new RuntimeException("GetVideoPlayAuthRequest Server failed");
    } catch (ClientException e) {
    	e.printStackTrace();
        throw new RuntimeException("GetVideoPlayAuthRequest Client failed");
    }
    	
    	 return response;
    }
    
	public IGoodsCourseItemService getGoodsCourseItemService() {
		return goodsCourseItemService;
	}

	public void setGoodsCourseItemService(
			IGoodsCourseItemService goodsCourseItemService) {
		this.goodsCourseItemService = goodsCourseItemService;
	}

}
